package com.guiji.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.guiji.constant.Constant;
import com.guiji.entity.CaseParams;
import com.guiji.entity.Environment;
import com.guiji.entity.HttpResult;
import com.guiji.entity.TestCase;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.testng.Assert;

import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: Jodie
 * @time: 2022/11/17
 */

@Slf4j
public class ParamUtils {

    public static String regexReplace(String orgStr) {
        try {
            if (orgStr != null) {
                Pattern pattern = Pattern.compile("\\$\\{(.*?)\\}");
                Matcher matcher = pattern.matcher(orgStr);
                while (matcher.find()) {
                    String allFindStr = matcher.group(0);
                    String innerStr = matcher.group(1);
                    Object replaceStr = MapUtils.getObject(Environment.map, innerStr, "");
                    orgStr = orgStr.replace(allFindStr, replaceStr.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orgStr;
    }

    public static void paramsReplace(TestCase testCase) {
        testCase.setHeaders(regexReplace(testCase.getHeaders()));
        testCase.setParams(regexReplace(testCase.getParams()));
        testCase.setUrl(Constant.BASE_URL.concat(regexReplace(testCase.getUrl())));
    }


    public static void extractValueFromRes(HttpResult httpResult, CaseParams caseParams) {
        if (StringUtils.isNotBlank(caseParams.getJsonCollect())) {
            JSONObject jsonObj = JSON.parseObject(caseParams.getJsonCollect());
            for (String key : jsonObj.keySet()) {
                String jsonpathExpression = jsonObj.get(key).toString();
                Object extractValue = JSONPath.read(httpResult.getMsg(), jsonpathExpression);
                if (Objects.nonNull(extractValue)) {
                    Environment.map.put(key, extractValue);
                }
            }
        }
    }

    public static boolean assertResponse(CaseParams caseParams, HttpResult httpResult) {
        try {
            if (StringUtils.isNotBlank(caseParams.getExpectedResult())) {
                JSONObject jsonObject = JSON.parseObject(caseParams.getExpectedResult());
                for (String k : jsonObject.keySet()) {
                    Object expectedValue = jsonObject.get(k);
                    Object actualValue = JSONPath.read(httpResult.getMsg(), k);
                    log.info("期望值:{}->实际值:{}", expectedValue, actualValue);
                    Assert.assertEquals(actualValue, expectedValue);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
